package util;

import java.util.List;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;


public class BaseDAO {
    public List executeHQLQuery(String hql){
        try{
           Session session = HibernateUtil.getSessionFactory().openSession();
           session.beginTransaction();
           Query q = session.createQuery(hql);
           List resultList = q.list();
           session.getTransaction().commit();
           session.close();
           return resultList;
        }
        catch(HibernateException he){
            return null;
        }
    }
    
    
    public <T> boolean executeInsertHQLQuery(T c){
        try{
            Session session = HibernateUtil.getSessionFactory().openSession();
            session.beginTransaction();
            session.save(c);
            session.getTransaction().commit();
            session.close();
            return true;
        }
        catch(HibernateException he){
            return false;
        }
    }
    
    public <T> boolean executeUpdateHQLQuery(T c){
        try{
            Session session = HibernateUtil.getSessionFactory().openSession();
            session.beginTransaction();
            session.update(c);
            session.getTransaction().commit();
            session.close();
            return true;
        }
        catch(HibernateException he){
            return false;
        }
    }
    
    public <T> boolean executeDeleteHQLQuery(T c){
        try{
            Session session = HibernateUtil.getSessionFactory().openSession();
            session.beginTransaction();
            session.delete(c);
            session.getTransaction().commit();
            session.close();
            return true;
        }
        catch(HibernateException he){
            return false;
        }
    }
    
    public List executeLimitHQLQuery(String hql, int intStart, int intMax){
        try{
           Session session = HibernateUtil.getSessionFactory().openSession();
           session.beginTransaction();
           Query q = session.createQuery(hql);
           q.setFirstResult(intStart);
           q.setMaxResults(intMax);
           List resultList = q.list();
           session.getTransaction().commit();
           session.close();
           return resultList;
        }
        catch(HibernateException he){
            return null;
        }
    }
    
}
